home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / prog / asmclock.arj / ASMCLOCK.ASM next >
Assembly Source File  |  1991-05-16  |  15KB  |  380 lines

  1. ;----------------------------------------------------------------------------;
  2. ;                               ASMCLOCK.ASM                                 ;
  3. ;----------------------------------------------------------------------------;
  4. ;                                                                            ;
  5. ; MASM 6.0 Translation of Charles Petzold's DIGCLOCK.C Digital Clock Program ;
  6. ;                                                                            ;
  7. ;----------------------------------------------------------------------------;
  8.  
  9.                 .8086
  10.                 .model  small, pascal, os_dos
  11.  
  12.                 include windows.inc               ; Converted from WINDOWS.H
  13.                 include time.inc                  ; Converted w/H2INC
  14.                 include stdio.inc                 ; Converted w/H2INC
  15.  
  16.                                                   ;------ PROLOGUE.INC ------;
  17. ?WP_INCBP       = 1                               ; INC  BP on all far procs ;
  18. ?WP_LOADDS      = 1                               ; LOAD DS on all far procs ;
  19.                 include prologue.inc              ;--------------------------;
  20.  
  21. ;----------------------------------------------------------------------------;
  22. ;                                Prototypes                                  ;
  23. ;                                                                            ;
  24. ; Function/Procedure prototypes are new to MASM 6.0.  They have the same     ;
  25. ; pupose as do prototypes in other High-Level languages.  Note the new       ;
  26. ; data initializers and the user defined types (HWND, and PINT).             ;
  27. ;----------------------------------------------------------------------------;
  28.  
  29. WndProc          PROTO FAR PASCAL,  :HWND, :WORD, :SWORD, :SDWORD
  30. SizeTheWindow    PROTO,             :PINT, :PINT, :PINT,  :PINT
  31. SetInternational PROTO
  32.  
  33. ;----------------------------------------------------------------------------;
  34. ;                             Numeric Equates                                ;
  35. ;                                                                            ;
  36. ; EQU is now recommended to be used with numeric equates only.  If you need  ;
  37. ; text equates, use the new directive, TEXTEQU.  This removes a lot of the   ;
  38. ; ambiguities associated with EQU for both text and numbers.                 ;
  39. ;----------------------------------------------------------------------------;
  40.  
  41. AMPMBUFLEN       EQU    5
  42. BUFLEN           EQU    25
  43.  
  44. ;----------------------------------------------------------------------------;
  45. ;                               Data Segment                                 ;
  46. ;                                                                            ;
  47. ; Note the new data type mnemonics BYTE (unsigned byte), and SBYTE (signed). ;
  48. ; There are also WORD, REAL4, REAL8, and, REAL10.  These along with the new  ;
  49. ; TYPEDEF keyword you can create your own data types as you can with other   ;
  50. ; High-Level languages.                                                      ;
  51. ;----------------------------------------------------------------------------;
  52.  
  53.                 .data
  54.  
  55. sDate           SBYTE   ?,?
  56. sTime           SBYTE   ?,?
  57. sAMPM           SBYTE   2 dup (AMPMBUFLEN dup(?))
  58. iDate           SWORD   ?
  59. iTime           SWORD   ?
  60.  
  61. szAppName       BYTE    "MASMClock",0
  62. szTooManyTimers BYTE    "Too many clocks or timers!",0
  63. szDisplay       BYTE    "DISPLAY", 0
  64. szIntl          BYTE    "intl", 0
  65. sziDate         BYTE    "iDate", 0
  66. sziTime         BYTE    "iTime", 0
  67. szsDate         BYTE    "sDate", 0
  68. szsTime         BYTE    "sTime", 0
  69. szs1159         BYTE    "s1159", 0
  70. szs2359         BYTE    "s2359", 0
  71. szAM            BYTE    "am", 0
  72. szPM            BYTE    "pm", 0
  73. szSlash         BYTE    "/", 0
  74. szColon         BYTE    ":", 0
  75. szWday          SBYTE    "Sun",0,"Mon",0,"Tue",0,"Wed",0,"Thu",0,"Fri",0,"Sat",0
  76. DateFmt         SBYTE    " %s  %d%s%02d%s%02d ",13,10,0
  77. TimeFmt1        SBYTE    " %02d%s%02d%s%02d ",0
  78. TimeFmt2        SBYTE    " %d%s%02d%s%02d %s ",0
  79.  
  80. ;----------------------------------------------------------------------------;
  81. ;                               Code Segment                                 ;
  82. ;----------------------------------------------------------------------------;
  83.  
  84.                 .code
  85.  
  86. extern      __ACRTUSED:ABS                  ; Let the linker know we use the
  87.                                             ; C runtimes.
  88.  
  89. ;----------------------------------------------------------------------------;
  90. ;                               WinMain                                      ;
  91. ;----------------------------------------------------------------------------;
  92.  
  93. WinMain     PROC,    hInstance:HANDLE,  hPrevInstance:HANDLE,
  94.                         lpszCmdLine:LPSTR, nCmdShow:SWORD
  95.                 LOCAL   msg:MSG,
  96.                         xStart:SWORD, yStart:SWORD, xClient:SWORD, yClient:SWORD,
  97.                         wndclass:WNDCLASS
  98. ;
  99. ;--- Check for previous instances
  100. ;
  101.                 .IF (hPrevInstance == 0)
  102.  
  103.                 mov wndclass.style, (CS_HREDRAW OR CS_VREDRAW)
  104.                 mov WORD PTR wndclass.lpfnWndProc,   LROFFSET WndProc
  105.                 mov WORD PTR wndclass.lpfnWndProc+2, SEG WndProc
  106.                 xor ax,ax
  107.                 mov wndclass.cbClsExtra, ax
  108.                 mov wndclass.cbWndExtra, ax
  109.                 mov ax, hInstance
  110.                 mov wndclass.hInstance, ax
  111.  
  112.                 INVOKE  LoadCursor, NULL, IDC_ARROW
  113.                 mov wndclass.hCursor, ax
  114.  
  115.                 INVOKE  GetStockObject, WHITE_BRUSH
  116.                 mov wndclass.hbrBackground, ax
  117.  
  118.                 xor ax, ax
  119.                 mov WORD PTR wndclass.lpszMenuName,   ax
  120.                 mov WORD PTR wndclass.lpszMenuName+2, ax
  121.  
  122.                 mov WORD PTR wndclass.lpszClassName,   OFFSET szAppName
  123.                 mov WORD PTR wndclass.lpszClassName+2, ds
  124.  
  125.                 INVOKE  RegisterClass, ADDR wndclass
  126.                 .IF (ax == 0)
  127.                 mov ax, FALSE
  128.                 jmp   doRet
  129.                 .ENDIF
  130.  
  131.                 .ENDIF     ;--- End of IF (hPrevInstance == 0)
  132.  
  133.                 INVOKE  SizeTheWindow, ADDR xStart,  ADDR yStart,
  134.                                        ADDR xClient, ADDR yClient
  135.  
  136.                 mov     ax, offset szAppName
  137.                 INVOKE  CreateWindow,  ds::ax, ds::ax,
  138.                                        WS_POPUP OR WS_DLGFRAME OR WS_SYSMENU,
  139.                                        xStart,  yStart,
  140.                                        xClient, yClient,
  141.                                        NULL, NULL,
  142.                                        hInstance,
  143.                                        NULL
  144.  
  145.                 mov     si, ax          ; keep hWnd in SI
  146.  
  147.                 INVOKE  SetTimer,      si, 1, 1000, NULL
  148.                 .IF (ax == 0)
  149.                 INVOKE  MessageBox,    si,
  150.                                        ADDR szTooManyTimers,
  151.                                        ADDR szAppName,
  152.                                        MB_ICONEXCLAMATION OR MB_OK
  153.                 mov     ax, FALSE
  154.                 jmp     doRet
  155.                 .ENDIF
  156.  
  157.                 INVOKE  ShowWindow,    si, SW_SHOWNOACTIVATE
  158.                 INVOKE  UpdateWindow,  si
  159.  
  160. ;
  161. ;----Message Loop
  162. ;
  163.  
  164.                 .WHILE TRUE
  165.                 INVOKE  GetMessage,    ADDR msg, NULL, 0, 0
  166.                 .BREAK .IF (ax == 0)
  167.  
  168.                 INVOKE  TranslateMessage, ADDR msg
  169.                 INVOKE  DispatchMessage,  ADDR msg
  170.                 .ENDW
  171.  
  172. ;
  173. ;---Return to Windows
  174. ;
  175.  
  176.                 mov     ax, msg.wParam
  177. doRet:
  178.                 ret
  179.  
  180. WinMain         ENDP
  181.  
  182. ;----------------------------------------------------------------------------;
  183. ;                              SizeTheWindow                                 ;
  184. ;----------------------------------------------------------------------------;
  185.  
  186. SizeTheWindow   PROC USES si di, pxStart:PINT, pyStart:PINT, pxClient:PINT, pyClient:PINT
  187.                 LOCAL tmetric:TEXTMETRIC
  188.  
  189.                 xor     dx, dx
  190.                 INVOKE  CreateIC, ADDR szDisplay, dx::dx, dx::dx, dx::dx
  191.                 INVOKE  GetTextMetrics, ax, ADDR tmetric
  192.                 INVOKE  DeleteDC, ax
  193.  
  194.                 INVOKE  GetSystemMetrics, SM_CXDLGFRAME       ; Set width
  195.                 shl     ax, 1
  196.                 mov     bx, tmetric.tmAveCharWidth
  197.                 mov     cl, 4
  198.                 shl     bx, cl
  199.                 add     ax, bx
  200.                 mov     si, pxClient
  201.                 mov     [si], ax
  202.  
  203.                 INVOKE  GetSystemMetrics, SM_CXSCREEN
  204.                 mov     si, pxClient
  205.                 sub     ax, [si]
  206.                 mov     si, pxStart
  207.                 mov     [si], ax
  208.  
  209.                 INVOKE  GetSystemMetrics, SM_CYDLGFRAME      ; Set Height
  210.                 shl     ax, 1
  211.                 mov     bx, tmetric.tmHeight
  212.                 shl     bx, 1
  213.                 add     ax, bx
  214.                 mov     si, pyClient
  215.                 mov     [si], ax
  216.  
  217.                 INVOKE  GetSystemMetrics, SM_CYSCREEN
  218.                 mov     si, pyClient
  219.                 sub     ax, [si]
  220.                 mov     si, pyStart
  221.                 mov     [si], ax
  222.  
  223.                 ret
  224.  
  225. SizeTheWindow   ENDP
  226.  
  227. ;----------------------------------------------------------------------------;
  228. ;                               SetInternational                             ;
  229. ;----------------------------------------------------------------------------;
  230.  
  231. SetInternational PROC
  232.  
  233.                  xor     dx, dx
  234.                  INVOKE  GetProfileInt, ADDR szIntl, ADDR sziDate, dx
  235.                  mov     iDate, ax
  236.                  INVOKE  GetProfileInt, ADDR szIntl, ADDR sziTime, 0
  237.                  mov     iTime, ax
  238.                  INVOKE  GetProfileString, ADDR szIntl, ADDR szsDate, ADDR szSlash, ADDR sDate, 2
  239.                  INVOKE  GetProfileString, ADDR szIntl, ADDR szsTime, ADDR szColon, ADDR sTime, 2
  240.                  INVOKE  GetProfileString, ADDR szIntl, ADDR szs1159, ADDR szAM,    ADDR sAMPM, AMPMBUFLEN
  241.                  INVOKE  GetProfileString, ADDR szIntl, ADDR szs2359, ADDR szPM, ADDR sAMPM+AMPMBUFLEN, AMPMBUFLEN
  242.  
  243.                  ret
  244.  
  245. SetInternational ENDP
  246.  
  247. ;----------------------------------------------------------------------------;
  248. ;                                  WndPaint                                  ;
  249. ;----------------------------------------------------------------------------;
  250.  
  251. WndPaint        PROC USES si di,  hWnd:HWND, hDC:HDC
  252.                 LOCAL   cBuffer[40]:BYTE,
  253.                         lTime:SDWORD,
  254.                         rect:RECT,
  255.                         nLength:SWORD,
  256.                         datetime:PTR tm
  257.  
  258.                 xor     dx, dx
  259.                 INVOKE  time, ADDR lTime                ; C "time" runtime
  260.                 INVOKE  localtime, ADDR lTime           ; C "localtime" runtime
  261.                 mov     datetime, ax
  262.  
  263.                 mov     bx, ax            ; WDAY * 4
  264.  
  265.                 ASSUME  bx:PTR tm                       ; Assumes that bx is
  266.                                                         ; a pointer to the
  267.                                                         ; structure "tm"
  268.                 mov     si, [bx].tm_wday
  269.                 shl     si, 1
  270.                 shl     si, 1
  271.  
  272.                 .IF (iDate == 1)                        ; cx == 1st date pos
  273.                 mov cx, [bx].tm_mday                    ; dx == 2nd date pos
  274.                 mov dx, [bx].tm_mon                     ; di == 3rd date pos
  275.                 inc dx
  276.                 mov di, [bx].tm_year
  277.                 .ELSEIF (iDate == 2)
  278.                 mov cx, [bx].tm_year
  279.                 mov dx, [bx].tm_mon
  280.                 inc dx
  281.                 mov di, [bx].tm_mday
  282.                 .ELSE
  283.                 mov cx, [bx].tm_mon
  284.                 inc cx
  285.                 mov dx, [bx].tm_mday
  286.                 mov di, [bx].tm_year
  287.                 .ENDIF
  288.  
  289.                 INVOKE  sprintf, ADDR cBuffer, ADDR DateFmt , ADDR szWday[si],
  290.                                  cx, ADDR sDate, dx, ADDR sDate, di
  291.                 mov nLength, ax
  292.  
  293.                 mov     bx, datetime
  294.                 mov     si, nLength
  295.  
  296.                 .IF (iTime == 1)
  297.                 INVOKE  sprintf, ADDR cBuffer[si], ADDR TimeFmt1, [bx].tm_hour,
  298.                                  ADDR sTime, [bx].tm_min, ADDR sTime, [bx].tm_sec
  299.                 add     nLength,ax
  300.                 .ELSE
  301.  
  302.                 mov  ax, [bx].tm_hour
  303.                 push ax
  304.                 xor  dx, dx
  305.                 mov  di, 12
  306.                 div  di
  307.                 xor  dx, dx
  308.                 mov  cx, AMPMBUFLEN
  309.                 mul  cx
  310.                 mov  di, ax
  311.  
  312.                 pop  ax                               ; ax == [bx].tm_hour
  313.                 xor  dx, dx
  314.                 mov  cx, 12
  315.                 div  cx
  316.                 .IF (dx == 0)
  317.                    mov cx, 12
  318.                 .ELSE
  319.                    mov cx, dx
  320.                 .ENDIF
  321.  
  322.                 INVOKE  sprintf, ADDR cBuffer[si], ADDR TimeFmt2,
  323.                                  cx, ADDR sTime, [bx].tm_min, ADDR sTime, [bx].tm_sec,
  324.                                  ADDR sAMPM[di]
  325.                 .ENDIF
  326.  
  327.                 ASSUME bx:NOTHING
  328.  
  329.                 INVOKE  GetClientRect, hWnd, ADDR rect
  330.                 INVOKE  DrawText,      hDC,  ADDR cBuffer, -1, ADDR rect,
  331.                                        (DT_CENTER OR DT_NOCLIP)
  332.  
  333.                 ret
  334.  
  335. WndPaint        ENDP
  336.  
  337. ;----------------------------------------------------------------------------;
  338. ;                                  WndProc                                   ;
  339. ;----------------------------------------------------------------------------;
  340.  
  341. WndProc         PROC FAR PASCAL, hWnd:HWND, iMessage:WORD, wParam:SWORD, lParam:SDWORD
  342.                 LOCAL   hDC:HDC, ps:PAINTSTRUCT
  343.  
  344.                 .IF     (iMessage == WM_CREATE)
  345.                 INVOKE SetInternational
  346.  
  347.                 .ELSEIF (iMessage == WM_TIMER)
  348.                 INVOKE  InvalidateRect, hWnd, NULL, FALSE
  349.  
  350.                 .ELSEIF (iMessage == WM_PAINT)
  351.                 INVOKE  BeginPaint, hWnd, ADDR ps
  352.                 mov     hDC, ax
  353.                 INVOKE  WndPaint, hWnd, hDC
  354.                 INVOKE  EndPaint, hWnd, ADDR ps
  355.  
  356.                 .ELSEIF (iMessage == WM_WININICHANGE)
  357.                 INVOKE  SetInternational
  358.                 INVOKE  InvalidateRect, hWnd, NULL, TRUE
  359.  
  360.                 .ELSEIF (iMessage == WM_DESTROY)
  361.                 INVOKE  KillTimer, hWnd, 1
  362.                 INVOKE  PostQuitMessage, 0
  363.  
  364.                 .ELSE
  365.                 INVOKE  DefWindowProc, hWnd, iMessage, wParam, lParam
  366.                 jmp     doRet
  367.  
  368.                 .ENDIF
  369.  
  370.                 mov ax, 0
  371.                 cwd
  372. doRet:
  373.                 ret
  374.  
  375. WndProc         ENDP
  376.  
  377. ;----------------------------------------------------------------------------
  378.  
  379.                 END
  380.